<?php
global $_GPC, $_W;
$GLOBALS['frames'] = $this->getMainMenu();
$branch_list = pdo_getall('yzzc_sun_branch',['uniacid'=>$_W['uniacid']],['id','name']);
$shuttle_list = pdo_getall('yzzc_sun_shuttle',['uniacid'=>$_W['uniacid']],['id','name']);
$where = "where uniacid =" . $_W['uniacid'];
$keywords = $_GPC['keywords'] ?? '';
if ($keywords) {
    $where .= " and (car_num LIKE  '%$keywords%')";
}

$range_time = $_GPC['range_time'] ?? [];
$start_time = $end_time = '';
if ($range_time) {
    $start_time = $range_time['start'];
    $end_time = $range_time['end'];
    if ($start_time && $end_time) {
        $end_time = strtotime($end_time);
        $start_time = strtotime($start_time);
        $where .= ' and createtime > ' . $start_time . ' and createtime<=' . $end_time;
    }
}
$page = max(1, intval($_GPC['page']));
$size = intval($_GPC['psize']) ? intval($_GPC['psize']) : 10;
$sql_page = 'SELECT * FROM ' . tablename('yzzc_sun_exterior') . " {$where} ORDER BY createtime DESC LIMIT " . (($page - 1) * $size) . ',' . $size;
$info = pdo_fetchall($sql_page);
$total_cost_money = $total_profit_ = 0;
if ($info){
    foreach ($info as $key => $value){
        $info[$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
        $cost_money = $value['money'] + $value['exceed_km'] + $value['gasoline_subsidy_cost'] + $value['car_damage_cost'];
        $total_profit = $value['sales_price'] - $cost_money;

        $info[$key]['total_profit'] = $total_profit;
        $info[$key]['total_cost_money'] = $cost_money;

        $total_cost_money+=$cost_money;
        $total_profit_+=$total_profit;
    }
}

$total = pdo_fetchcolumn("select count(*) from " . tablename("yzzc_sun_shuttle_income") . " where uniacid =" . $_W['uniacid']);
$pager = pagination($total, $page, $size);

if ($_GPC['op'] == 'add'){
    if (checksubmit('submit')) {
        if (empty($_GPC['car_name'])) {
            message('车型名称不能为空', $this->createWebUrl('exterior'), 'error');
        }
        if (empty($_GPC['sales_price'])) {
            message('发车价格不能为空', $this->createWebUrl('exterior'), 'error');
        }
        if (empty($_GPC['money'])) {
            message('外调租金不能为空', $this->createWebUrl('exterior'), 'error');
        }
        $data = [
            'car_name' => trim($_GPC['car_name']),
            'car_num' => trim($_GPC['car_num']),
            'uniacid' => $_W['uniacid'],
            'sales_price' => $_GPC['sales_price'],
            'money' => $_GPC['money'],
            'exceed_km' => $_GPC['exceed_km'],
            'gasoline_subsidy_cost' => $_GPC['gasoline_subsidy_cost'],
            'car_damage_cost' => $_GPC['car_damage_cost'],
            'createtime' => time()
        ];
        $result = pdo_insert('yzzc_sun_exterior', $data);
        if ($result) {
            message('提交成功', $this->createWebUrl('exterior', array()), 'success');
        } else {
            message('提交失败', $this->createWebUrl('exterior'), 'error');
        }
    }
}
if ($_GPC['op'] == 'delete') {
    $is = pdo_get('yzzc_sun_goods', array('sid' => $_GPC['id']));

    if ($is) {
        message('请先至车辆列表下架所有车辆才能删除', '', 'error');
    } else {
        $branch_info = pdo_get('yzzc_sun_branch', array('id' => $_GPC['id']));
        $res = pdo_delete('yzzc_sun_branch', array('id' => $_GPC['id']));
        if ($res) {
            if ($branch_info['type'] != 1){
                pdo_update('yzzc_sun_store',['status'=>0],['store_id'=>$branch_info['store_id']]);
            }
            message('操作成功', $this->createWebUrl('branchslist', array()), 'success');
        } else {
            message('操作失败', '', 'error');
        }
    }
}

include $this->template('web/exterior');